<?php


class AdminAction extends SecurityAction{
	function _initialize(){
        $this->assign("menuFirst",session('menuFirst')); 
        $this->assign("menuSecond",session('menuSecond')); 
    }
		
	function alist(){
		if ($_GET['opt'] == 'add'){  //新增跳转
			$ob = M("yq_shop");
			$shoplist = $ob->where(array("is_forbit"=>"1"))->field("tid,shopname")->select();
			if(empty($shoplist)){
				$this->error('没有门店，请添加！','/Admin/alist');
				return;
			}

			$role = M("yq_role");
			$rolelist = $role->where(array("is_forbit"=>"1"))->field("tid,rolename")->select();
			if (empty($rolelist)){
				$this->error("尚未初始化角色","/Admin/alist'");
				return;
			}
			$this->assign("rolelist", $rolelist);
			$this->assign("shoplist", $shoplist);
			$this->display("Admin/detail");
			return;
		}


	    if ($_GET["opt"] == 'edit'){   //编辑

	    	$ob = M("yq_shop");
			$shoplist = $ob->where(array("is_forbit"=>"1"))->field("tid,shopname")->select();

			$role = M("yq_role");
			$rolelist = $role->where(array("is_forbit"=>"1"))->field("tid,rolename")->select();
			if (empty($rolelist)){
				$this->error("尚未初始化角色","/Admin/alist'");
				return;
			}

	        $ob = M("yq_admin");
	        $info = $ob->where(array("tid"=>$_GET["tid"]))->find();
	        $this->assign("modify", '1');
	        $this->assign("info", $info);

	        $this->assign("rolelist", $rolelist);
	        $this->assign("shoplist", $shoplist);
	        $this->display('Admin/detail');
	        return;
	    }

		$ob = M("yq_admin");
        
	/*	if(session('info.roleid')=='1'){
		    //总经理
		    $sql = array();
		}elseif(session('info.roleid')=='5'){
		    //客服
		    $sql = array('roleid'=>'5','tid'=>session('info.tid'));
		}else{
		    $sql = array('roleid'=>'0000000','tid'=>session('info.tid'));
		}*/
		
		if($_GET['account']){
		    $sql[] = array('yq_admin.account'=>array('like',"%{$_GET['account']}%"));
		}
		
		$cpp = 30; //分页数
        import('ORG.Util.Page');// 导入分页类
        $count      = $ob->where($sql)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        
		$list = $ob->join("yq_shop on yq_admin.shopid = yq_shop.tid ")->join("yq_role on yq_admin.roleid = yq_role.tid")->field("yq_admin.tid,yq_admin.account,yq_admin.password,yq_admin.nickname,yq_admin.realname,yq_admin.roleid,yq_admin.shopid,yq_admin.is_forbit,yq_admin.last_ip,yq_admin.remark,yq_admin.add_time,yq_admin.last_time,yq_shop.shopname,yq_role.rolename")->where($sql)->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign("list", $list);
		$this->assign('show',$show);// 赋值分页输出

		$this->display("Admin/alist");
	}
	

	function detail(){
	    
	    if($_POST){
	        //添加
	        if ($_POST['password']){
	            $_POST['password'] = md5($_POST['password']);
	        }else{
	            unset($_POST['password']);
	        }

	        $_POST['adminid'] = session('info.tid');
	        $_POST['add_time'] = time();//date('Y-m-d H:i:s', time());
	        $_POST['last_ip'] = $_SERVER["REMOTE_ADDR"];
	        $ob = M("yq_admin");
	    
	        if($_POST['tid'] != ''){
	            $ob->where(array('tid'=>$_POST['tid']))->save($_POST);
	        }else{
	            $info = $ob->where(array('account'=>$_POST['account']))->find();
	            if(!empty($info)){
	                $this->error('登录账号重复，请重新添加','/Admin/detail');
	                return;
	            }
	            $ob->add($_POST);
	        }
	        	
	        $this->redirect('/Admin/alist/');
	        return;
	    }
	}


	//修改当前登录人密码
	public function updatePass(){
		//修改密码
		if ($_POST){
			$ob = M("yq_admin");

			$_POST["password"] = md5($_POST["password"]);

			$ob->where(array("tid" =>$_POST["tid"]))->save($_POST);
			$this->redirect("/Admin/alist");
			return;
		}

		//修改菜单跳转修改页面
		$info["tid"] = session('info.tid');
		$info["account"] = session('info.account');
		$this->assign("info",$info);
		$this->display("/Admin/personInfo");
	}
	
	function shopmember(){
	    if ($_GET['opt'] == 'add'){  //新增跳转
	        $ob = M("yq_shop");
	        $shoplist = $ob->where(array("is_forbit"=>"1"))->field("tid,shopname")->select();
	        if(empty($shoplist)){
	            $this->error('没有门店，请添加！','/Admin/alist');
	            return;
	        }
	    
	        $role = M("yq_role");
	        $rolelist = $role->where(array("is_forbit"=>"1"))->field("tid,rolename")->select();
	        if (empty($rolelist)){
	            $this->error("尚未初始化角色","/Admin/alist'");
	            return;
	        }
	        $this->assign("rolelist", $rolelist);
	        $this->assign("shoplist", $shoplist);
	        $this->display("Admin/shopdetail");
	        return;
	    }
	    
	    
	    if ($_GET["opt"] == 'edit'){   //编辑
	    
	        $ob = M("yq_shop");
	        $shoplist = $ob->where(array("is_forbit"=>"1"))->field("tid,shopname")->select();
	    
	        $role = M("yq_role");
	        $rolelist = $role->where(array("is_forbit"=>"1"))->field("tid,rolename")->select();
	        if (empty($rolelist)){
	            $this->error("尚未初始化角色","/Admin/shopmember'");
	            return;
	        }
	    
	        $ob = M("yq_member");
	        $info = $ob->where(array("tid"=>$_GET["tid"]))->find();
	        $this->assign("modify", '1');
	        $this->assign("info", $info);
	    
	        $this->assign("rolelist", $rolelist);
	        $this->assign("shoplist", $shoplist);
	        $this->display('Admin/shopdetail');
	        return;
	    }
	    
	    $ob = M("yq_member");
	    
	    /*	if(session('info.roleid')=='1'){
	     //总经理
	     $sql = array();
	     }elseif(session('info.roleid')=='5'){
	     //客服
	     $sql = array('roleid'=>'5','tid'=>session('info.tid'));
	     }else{
	     $sql = array('roleid'=>'0000000','tid'=>session('info.tid'));
	    }*/
	    
	    if($_GET['account']){
	        $sql[] = array('yq_member.account'=>array('like',"%{$_GET['account']}%"));
	    }
	    
	    $cpp = 30; //分页数
	    import('ORG.Util.Page');// 导入分页类
	    $count      = $ob->where($sql)->count();// 查询满足要求的总记录数
	    $Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
	    $show       = $Page->show();// 分页显示输出
	    
	    $list = $ob->join("yq_shop on yq_member.shopid = yq_shop.tid ")
// 	               ->join("yq_role on yq_member.roleid = yq_role.tid")
	                   ->field("yq_member.*,yq_shop.shopname")
	                           ->where($sql)
	                           ->limit($Page->firstRow.','.$Page->listRows)
	                               ->select();
	    
	    $this->assign("list", $list);
	    $this->assign('show',$show);// 赋值分页输出
	    
	    $this->display("Admin/shopmember");
	}
	
	function shopdetail(){
		 
		if($_POST){
			//添加
			if ($_POST['passwd']){
				$_POST['passwd'] = md5($_POST['passwd']);
			}else{
				unset($_POST['passwd']);
			}
	
// 			$_POST['adminid'] = session('info.tid');
// 			$_POST['add_time'] = time();//date('Y-m-d H:i:s', time());
// 			$_POST['last_ip'] = $_SERVER["REMOTE_ADDR"];
			$ob = M("yq_member");
		  	
			if($_POST['tid'] != ''){
				$ob->where(array('tid'=>$_POST['tid']))->save($_POST);
			}else{
				$info = $ob->where(array('account'=>$_POST['account']))->find();
				if(!empty($info)){
					$this->error('登录账号重复，请重新添加','/Admin/shopdetail');
					return;
				}
				$ob->add($_POST);
			}
	
			$this->redirect('/Admin/shopmember/');
			return;
		}
	}
}

?>